# coding: utf-8
"""
获取历史文章链接
"""
import json
import os
import time

from wechatarticles.utils import end_func, save_f, get_history_urls

"""
url

1. 文章html、pdf
2. 阅读点赞
"""

# 是否设定截止日期
date_condition = False
dt2stamp = lambda dt: time.mktime(time.strptime(dt, "%Y-%m-%d"))
dt = "2020-01-01"
start_timestamp = dt2stamp(dt)

# 设定参数文件
with open("params/biz_name_key.txt", "r", encoding="utf-8") as f:
    data = f.readlines()
biz, name, key, uin = (item.strip() for item in data)

if __name__ == "__main__":
    lst = get_history_urls(
        biz, uin, key, lst=[], start_timestamp=0, count=0, endcount=99999
    )

    # 防止重名覆盖
    if not os.path.isfile("{}.json".format(name)):
        with open("{}.json".format(name), "w", encoding="utf-8") as f:
            for line in lst:
                for item in line:
                    f.write("'{}'\n".format(json.dumps(item)))
    else:
        name = save_f(name)
        with open("{}.json".format(name), "w", encoding="utf-8") as f:
            for line in lst:
                for item in line:
                    f.write("'{}'\n".format(json.dumps(item)))
